package top.goldenyear.porpoise.infra.job.util;

import java.util.Map;
import org.noear.solon.scheduling.ScheduledException;
import org.noear.solon.scheduling.annotation.Scheduled;
import org.noear.solon.scheduling.scheduled.JobHandler;
import org.noear.solon.scheduling.scheduled.JobHolder;
import org.noear.solon.scheduling.simple.JobManager;

/**
 * @author airhead
 */
public class JobManagerUtils {

  /**
   * 添加任务（不需要再 jobStart）
   *
   * @param name
   * @param scheduled
   * @param handler
   * @return
   */
  public static JobHolder add(String name, Scheduled scheduled, JobHandler handler) {
    return JobManager.getInstance().jobAdd(name, scheduled, handler);
  }

  /**
   * 任务是否存在
   *
   * @param name
   * @return
   */
  public static boolean exists(String name) {
    return JobManager.getInstance().jobExists(name);
  }

  /**
   * 获取任务
   *
   * @param name
   * @return
   */
  public static JobHolder get(String name) {
    return JobManager.getInstance().jobGet(name);
  }

  /**
   * 移除任务
   *
   * @param name
   */
  public static void remove(String name) {
    JobManager.getInstance().jobRemove(name);
  }

  /**
   * 启动任务
   *
   * @param name
   * @param data
   * @throws ScheduledException
   */
  public static void start(String name, Map<String, String> data) throws ScheduledException {
    JobManager.getInstance().jobStart(name, data);
  }

  /**
   * 停止任务
   *
   * @param name
   * @throws ScheduledException
   */
  public static void stop(String name) throws ScheduledException {
    JobManager.getInstance().jobStop(name);
  }

  /**
   * 判断任务管理器是否启动
   *
   * @return
   */
  public boolean isStarted() {
    return JobManager.getInstance().isStarted();
  }
}
